{extend name="public/layout" /}
{block name="style"}
<style>
    .layui-table-page{
        text-align: center;
    }
    th{
        text-align: center;!important;
    }
    .chen{
        text-align: center;
    }
    /*.layui-table-cell{*/
    /*text-align: center;*/
    /*}*/
</style>
{/block}
{block name="script"}
{/block}
{block name="main"}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <div class="layui-row" style="width: 100%;margin-top: 15px;min-height: 40px">
        <!--表格上部分的标题-->
        {if $header['alldel']['show']}
        <!--添加全部删除按钮-->
        {include file="table/alldel"}
        {/if}
        {if $header['add']['show']}
        <!--添加添加按钮-->
        {include file="table/add"}
        {/if}
        {if $header['back']['show']}
        <!--添加返回按钮-->
        {include file="table/back"}
        {/if}
        {if $header['refresh']['show']}

        {include file="table/refresh"}
        {/if}
        {if $header['sousuo']['show']}
        <!--添加搜索框-->
        {include file="table/sousuo"}
        {/if}
        {if $header['text']['show']}
        <!--添加文本-->
        {include file="table/text"}
        {/if}
        {if isset($select)&&$select}
        <!--添加下拉框-->
        {include file="table/select"}
        {/if}
    </div>
</fieldset>
{if $page['show']}
<table class="layui-table" width="100%" lay-data="{ url:'{$page['action']}', page:{layout:['count', 'prev','page','next','limit','refresh','skip']}, id:'idTest',limits:[10,20,30,40,50,60,70,80,90,100]}" lay-filter="demo">
    {else}
    <table class="layui-table" width="100%" lay-data="{ url:'{$page['action']}', id:'idTest',limits:[10,20,30,40,50,60,70,80,90,100]}" lay-filter="demo">
    {/if}
    <thead>
    <tr>
        <th lay-data="{type:'checkbox', fixed: 'left',align:'center'}"></th>
        <!--<th lay-data="{field:'id' sort: true, fixed: true}">ID</th>-->
        {foreach $column as $value}
        {if $value['type']=='text'}
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}',width:{$value['width']},style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {elseif $value['type']=='switch' }
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',width:{$value['width']},style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
            {elseif $value['type']=='img' }
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',width:{$value['width']},style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {elseif $value['type']=='imgs' }
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',width:{$value['width']},style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {elseif $value['type']=='status' }
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',width:{$value['width']},style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {/if}
        {if $value['type']=='edit'}
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}',width:{$value['width']}, sort: true, edit: 'text',align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', sort: true, edit: 'text',align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {/if}
        {if $value['type']=='button'}
        {if $value['width']}
        <th lay-data="{field:'{$value['name']}', width:{$value['width']},templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {else}
        <th lay-data="{field:'{$value['name']}', templet:'#{$value['name']}',style:'{$value['style']}',sort: true,align:'{$value['align']}'}">{$value['desc']}</th>
        {/if}
        {/if}
        {/foreach}
        {if isset($right_header['show'])}
        {if isset($right_header['width'])}
        <th lay-data="{fixed: 'right', width:{$right_header['width']}, align:'center', toolbar: '#barDemo',align:'center'}">{$right_header['desc']}</th>
        {else}
        <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barDemo',align:'center'}">{$right_header['desc']}</th>
        {/if}
        {/if}
    </tr>
    </thead>
</table>
<div id="columns" style="display: none">{$columns}</div>
{include file='table/bardemo'}
    {include file='table/tabletext'}
    {/block}
    {block name="footer"}
    <script type="text/javascript" src="__HOME_JS__/jquery.3.3.1.min.js"></script>
    <script type="text/javascript" src="__HOME_LAYUI__/layui.all.js"></script>
    <script type="text/javascript" src="__HOME_JS__/webuploader.min.js"></script>
    <script type="text/javascript" src="__HOME_JS__/MaoChaJian.js"></script>
    <script type="text/javascript" src="__STATIC__/home/fuwenben/ueditor.config.js"></script>
    <script type="text/javascript" src="__STATIC__/home/fuwenben/ueditor.all.js"></script>
    {/block}
{block name="javascript"}
<script>
    layui.use('table', function(){
        var table = layui.table;
        var form = layui.form;
        //监听表格复选框选择
        table.on('checkbox(demo)', function(obj){
            console.log(obj)
        });

        /**
         * 重载数据
         */
        function onreloader(){
            //加载数据
            var tiaojian ={};
            tiaojian.tiaojian = $('#sousuo').val();
            var shuju = $("#soutiaojian").children();
            var curr = $(".layui-laypage-curr").children('em').eq(1).text();
            var name ='';
            $.each(shuju,function (i,v) {
                name = $(v).attr('name');
                tiaojian[name] = $(v).val();
            });
            console.log(tiaojian);
            table.reload('idTest', {
                page: {
                    curr: parseInt(curr) //重新从第 1 页开始
                }
                ,where: tiaojian
            });
        };
        //监听工具条
        /**
         * 监听
         */
        table.on('tool(demo)', function(obj){
            var data = obj.data;
            var action = $(this).attr('action');
            var v= /[?]/;
            if(v.test(action)){
                action = action+"&id="+data.id;
            }else {
                action = action+"?id="+data.id;
            }
            if(obj.event === 'detail'){
                console.log(action);
                layer.open({
                    type:2,
                    area:['80%','90%'],
                    content:action,
                    end:function () {

                    }
                })
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.post(action,{},function (res) {
                        console.log(res);
                        layer.close(index);
                        if(res.code==200){
                            layer.msg(res.msg);
                            onreloader();
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                });
            } else if(obj.event === 'edit'||obj.event==='addchild'){
                layer.open({
                    type:2,
                    area:['80%','90%'],
                    content:action,
                    end:function () {
                        {if $page['show']}
                        onreloader();
                        {else}
                        window.location.reload();
                        {/if}
                        }
                    })
                // layer.alert('编辑行：<br>'+ JSON.stringify(data))
            }else if(obj.event === 'button'){
                layer.confirm('要执行这个操作吗?', function(index){
                    $.post(action,{},function (res) {
                        layer.close(index);
                        console.log(res);
                        if(res.code==200){
                            layer.msg(res.msg);
                            onreloader();
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                });
            }
        });
        table.on('edit',function (obj) {
            console.log(obj);
            // console.log($(this))
            var id = obj.data.id;
            var field = obj.field;
            var value = obj.value;
            var action = '';
            var columns = eval('('+$("#columns").text()+')');
            $.each(columns,function (i,v) {
                if(v.name==field){
                    action = v.action;
                }
            })
            layer.confirm('真的确定修改?', function(index){
                $.post(action,{id:id,field:field,value:value},function (res) {
                    console.log(res)
                    if(res.code==200){
                        layer.msg('修改成功')
                    }else{
                        layer.msg('修改失败');
                    }
                })
            },function () {
                {if $page['show']}
                onreloader();
                {else}
                window.location.reload();
                {/if}
            });

        })
        table.on('click(bbus)',function (obj) {
            console.log(obj)
        })

        var $ = layui.$, active = {
            //全部删除函数
            getCheckData: function(){
                //获取选中数据
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                var id = [];
                $.each(data,function (i,v) {
                    id.push(v.id);
                });
                var action = $(this).attr('action');
                layer.confirm('真的删除行么', function(index){
                    $.post(action,{id:id},function (res) {
                        console.log(res);
                        layer.close(index);
                        if(res.code==200){
                            layer.msg(res.msg)
                            onreloader();
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                });

            }
            ,getCheckLength: function(){
                //获取选中数目
                var checkStatus = table.checkStatus('idTest')
                    ,data = checkStatus.data;
                layer.msg('选中了：'+ data.length + ' 个');
            }
            ,isAll: function(){
                //验证是否全选
                var checkStatus = table.checkStatus('idTest');
                layer.msg(checkStatus.isAll ? '全选': '未全选')
            },
            reload:function () {
                //搜索
                onreloader();
            },
            addTianjia:function () {
                var action = $(this).attr('action');
                    layer.open({
                        type:2,
                        area:['80%','90%'],
                        content:action,
                        end:function () {
                            {if $page['show']}
                            onreloader();
                            {else}
                            window.location.reload();
                            {/if}
                        }
                    })
            }
        };

        $('.demoTable').on('click', function(){
            var type = $(this).data('type');
            console.log(type);
            active[type] ? active[type].call(this) : '';
        });
        $(".xinclass").on('change',function () {
            var action = $(this).attr('data-action');
            console.log(action);
            var obj = $(this);
            var tiaojian ={};
            tiaojian.tiaojian = $('#sousuo').val();
            var shuju = $("#soutiaojian").children();
            var name ='';
            $.each(shuju,function (i,v) {
                name = $(v).attr('name');
                tiaojian[name] = $(v).val();
            });
            var sts = $(this).attr('shif');
            if(sts=='true'){
                $.post(action,tiaojian,function (res) {
                    if(res.code==200){
                        var data = res.data;
                        var str = '';
                        $.each(data,function (i,v) {
                            str +='<option value="'+i+'">'+v+'</option>';
                        });
                        obj.next('select').empty();
                        obj.next('select').append(str);
                        onreloader();
                    }else{
                        layer.msg(res.msg);
                    }
                })
            }else{
                onreloader();
            }
        });
        form.on('switch', function(obj){
            var action = this.name;
            console.log(action);
            var id = this.value;
            $.post(action,{id:id},function (res) {
                console.log(res);
                if(res.code==200){
                    onreloader();
                    layer.msg(res.msg);
                }else{
                    onreloader();
                    layer.msg(res.msg);
                }
            })
        });
    });

    //返回按钮函数
    function back(action) {
        console.log(action);
        if(action){
            window.location.href = action;
        }else{
            history.back(-1);
        }
    }
    function changeData(action,id,obj) {
        var that = $(obj);
        var text = that.attr('data-value');
        var field = that.attr('data-name');
        layer.confirm('你真的需要修改吗?', function(index){
            var indexs = layer.load(0, {shade: false});
            $.post(action,{id:id,value:text,field:field},function (res) {
                console.log(res);
                layer.close(indexs);
                layer.close(index);
                if(res.code==200){
                    layer.msg(res.msg)
                    that.text(res.data);
                    if(!res.data){
                        window.location.reload();
                    }
                }else{
                    layer.msg(res.msg);
                }
            })
        });
    }
</script>
    {/block}